www.gusucode.com > VC++ 端口扫描器Scan源码-源码程序 > VC++ 端口扫描器Scan源码-源码程序/code/CoolScan_第十组/code/JIEMIANDlg.cpp

    //Download by http://www.NewXing.com
// JIEMIANDlg.cpp : implementation file
//

#include "stdafx.h"
#include "JIEMIAN.h"
#include "JIEMIANDlg.h"
#include "Ping.h"
#include "SYNPing.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//Global Various
//Thread
int nThreadCount;
int m_nCounter;
CString strCurIP;
HWND hDlg;
bool bIsStopPing;

//Check Box
BOOL bIsScanByhand;
bool bIsSYN;
bool bIsShowResHost;

//Tree
CImageList m_ImageList;
CTreeCtrl	*m_GbTree;
HTREEITEM m_hRoot;
HTREEITEM m_hCurrentItem;
bool bIsFocused;

//IP
DWORD dStartIP,dEndIP,dCurIP;
CString dFileIP[1000];
int nLineCount;
BOOL bIsSpecScan;

//Port
int nStartPort,nEndPort;

//Radio
int nIpWay; //0代表单IP扫描,1代表多IP扫描,2代表文件导入IP扫描
BOOL  bIsConnect; //TRUE for Connect Method ,FALSE for SYN
BOOL  bIsSinglePort; //True for Single port ,FALSE for multi ports 

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJIEMIANDlg dialog

CJIEMIANDlg::CJIEMIANDlg(CWnd* pParent /*=NULL*/)
: CDialog(CJIEMIANDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJIEMIANDlg)
	m_iThCount = 0;
	m_bPingOnly = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CJIEMIANDlg::~CJIEMIANDlg()
{
	WSACleanup();
}
void CJIEMIANDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJIEMIANDlg)
	DDX_Control(pDX, IDC_STOP_SCAN, m_stopScan);
	DDX_Control(pDX, IDC_START_SCAN, m_startScan);
	DDX_Control(pDX, IDC_SCANPORT, m_scanPort);
	DDX_Control(pDX, IDC_SCANIP, m_scanIP);
	DDX_Control(pDX, IDC_PROGRESS1, m_progressScan);
	DDX_Control(pDX, IDC_PINGIP, m_pingIP);
	DDX_Control(pDX, IDC_IPADDRESS_FROM, m_sIP);
	DDX_Control(pDX, IDC_IPADDRESS_TO, m_eIP);
	DDX_Control(pDX, IDC_IPADDRESS1, m_SingleIP);
	DDX_Control(pDX, IDC_TREE1, m_Tree);
	DDX_Control(pDX, IDC_SLIDER2, m_Slider);
	DDX_Text(pDX, IDC_THREAD_COUNT, m_iThCount);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJIEMIANDlg, CDialog)
//{{AFX_MSG_MAP(CJIEMIANDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_MULTI_IP, OnMultiIp)
ON_BN_CLICKED(IDC_SINGLE_IP, OnSingleIp)
ON_BN_CLICKED(IDC_FILE_IP, OnFileIp)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
ON_BN_CLICKED(IDC_SINGLE_PORT, OnSinglePort)
ON_BN_CLICKED(IDC_MULTI_PORT, OnMultiPort)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
ON_BN_CLICKED(IDC_SCAN_BYHAND, OnScanByhand)
ON_BN_CLICKED(IDC_START_SCAN, OnStartScan)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_NOTIFY(NM_SETFOCUS, IDC_TREE1, OnSetfocusTree1)
ON_NOTIFY(NM_KILLFOCUS, IDC_TREE1, OnKillfocusTree1)
ON_BN_CLICKED(IDC_SCAN_SELECTED, OnScanSelected)
ON_BN_CLICKED(IDC_WAYSYN, OnWaysyn)
ON_BN_CLICKED(IDC_WAYCONN, OnWayconn)
ON_BN_CLICKED(IDC_STOP_SCAN, OnStopScan)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_SAVESCAN, OnSavescan)
	ON_BN_CLICKED(IDC_SHOW_RESHOST, OnShowReshost)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJIEMIANDlg message handlers

BOOL CJIEMIANDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	StateInital();
	
	
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJIEMIANDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJIEMIANDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CJIEMIANDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CJIEMIANDlg::OnMultiIp() 
{
	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_IPADDRESS1)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(TRUE);
	GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(TRUE);
	GetDlgItem(IDC_OPENFILE)->EnableWindow(FALSE);
	nIpWay = 1;
	
}

void CJIEMIANDlg::OnSingleIp() 
{
	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_IPADDRESS1)->EnableWindow(TRUE);
	GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(FALSE);
	GetDlgItem(IDC_OPENFILE)->EnableWindow(FALSE);
	nIpWay = 0;
	//	GetDlgItem(IDC_IPADDRESS1)->SetAddress()
	
}

void CJIEMIANDlg::OnFileIp() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_IPADDRESS1)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(FALSE);
	GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(FALSE);
	GetDlgItem(IDC_OPENFILE)->EnableWindow(TRUE);
	nIpWay =2;
	
}

void CJIEMIANDlg::OnOpenfile() 
{
	// TODO: Add your control notification handler code here
	
	CFileDialog IPFile(TRUE,"*.txt",NULL,OFN_ALLOWMULTISELECT,"IPfile(*.txt)|*.txt|All(*.*)|*.*||,NULL");
	if(IPFile.DoModal()==IDOK)
	{
		SetDlgItemText(IDC_IPFILE,IPFile.GetPathName());
	}
	
}

void CJIEMIANDlg::OnSinglePort() 
{
	// TODO: Add your control notification handler code here
	((CButton *)GetDlgItem(IDC_SINGLE_PORT))->SetCheck(TRUE);
	GetDlgItem(IDC_SPORT)->EnableWindow(false);
	GetDlgItem(IDC_EPORT)->EnableWindow(false);
	GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
	CString temp;
	GetDlgItem(IDC_PORT)->GetWindowText(temp);
	if (temp == "")
		SetDlgItemInt(IDC_PORT,0,TRUE);
	GetDlgItem(IDC_PORT)->SetFocus();
	
}

void CJIEMIANDlg::OnMultiPort() 
{
	// TODO: Add your control notification handler code here
	((CButton *)GetDlgItem(IDC_MULTI_PORT))->SetCheck(TRUE);
	GetDlgItem(IDC_SPORT)->EnableWindow(TRUE);
	GetDlgItem(IDC_EPORT)->EnableWindow(TRUE);
	GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
	CString temp;
	GetDlgItem(IDC_SPORT)->GetWindowText(temp);
	if (temp == "")
		SetDlgItemInt(IDC_SPORT,0,TRUE);
	GetDlgItem(IDC_EPORT)->GetWindowText(temp);
	if (temp == "")
		SetDlgItemInt(IDC_EPORT,0,TRUE);
	GetDlgItem(IDC_SPORT)->SetFocus();
	
}

void CJIEMIANDlg::OnCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int pos;
	pos = m_Slider.GetPos();
	m_iThCount =201 - pos;
	UpdateData(FALSE);
	nThreadCount = m_iThCount;
	*pResult = 0;
}


//************************************************************
//	函数名:StateInital
//	功能描述:对界面中用到的变量,函数等进行初始化
//	参数:无
//	返回:无
//************************************************************
void CJIEMIANDlg::StateInital()
{
	//Net
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
		AfxMessageBox("WSAStartup() failed");
        return ;
    }
	//Thread
	hDlg = GetSafeHwnd();
	dCurIP = 0;
	bIsStopPing = false;
	//Slider
	nThreadCount = m_iThCount = 50;
	m_Slider.SetRange(1,200);
	m_Slider.SetTicFreq(20);
	m_Slider.SetPos(201 - m_iThCount);
	
	//RadioBox
	((CButton *)GetDlgItem(IDC_WAYCONN))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_SINGLE_IP))->SetCheck(true);
	OnSingleIp();
	OnSinglePort();
	
	//CheckBox
	bIsScanByhand = FALSE;
	bIsShowResHost = false;
	bIsSYN = false;
	
	//Tree
	m_ImageList.Create(16, 16, ILC_COLORDDB | ILC_MASK, 0, 1);
	CBitmap bitHost,bitPort,bitLiveHost,bitDeadHost;
	bitHost.LoadBitmap(IDB_HOST);
	bitPort.LoadBitmap(IDB_LIVEPORT);
	bitLiveHost.LoadBitmap(IDB_LIVEHOST);
	bitDeadHost.LoadBitmap(IDB_DEADHOST);
	m_ImageList.Add(&bitHost, RGB(192,192,192));
	m_ImageList.Add(&bitPort , RGB(192,192,192));
	m_ImageList.Add(&bitLiveHost , RGB(192,192,192));
	m_ImageList.Add(&bitDeadHost , RGB(192,192,192));
	
	m_Tree.SetImageList(&m_ImageList ,TVSIL_NORMAL);
	m_hCurrentItem = m_hRoot = m_Tree.InsertItem("主机IP",0,0);
	
	m_GbTree = &m_Tree;
	bIsFocused = false;
	//Ip
	nLineCount = 0;
	bIsSpecScan =FALSE;
	m_nCounter = -1;
	
	//ProgressCtrl
	
	
	
	
	
}

//
//************************************************************
//	函数名:OnScanByhand
//	功能描述:用全局布尔变量控制
//	参数:无
//	返回:无
//************************************************************
void CJIEMIANDlg::OnScanByhand() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_SHOW_RESHOST)->EnableWindow(bIsScanByhand);
	GetDlgItem(IDC_SCAN_SELECTED)->EnableWindow(!bIsScanByhand);
	bIsScanByhand=!bIsScanByhand; //每选一次,取反
	if (!bIsScanByhand)
		bIsSpecScan = false;
}
//************************************************************
//	函数名:OnStartScan
//	功能描述:开始扫描
//	参数:无
//	返回:无
//************************************************************
void CJIEMIANDlg::OnStartScan() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_START_SCAN)->EnableWindow(FALSE);
	GetDlgItem(IDC_STOP_SCAN)->EnableWindow(true);
	if (!GetIP())
		return;
	if (!GetPort())
		return;
	nThreadCount = m_iThCount;
	m_nCounter = nStartPort;
	bIsSpecScan = false;
	bIsStopPing = false;
	m_GbTree->DeleteAllItems();
	m_hRoot = m_Tree.InsertItem("主机IP",0,0);
	m_progressScan.SetRange32(0,nEndPort - nStartPort + 1);
	m_progressScan.SetPos(0);
	m_progressScan.SetStep(1);
	AfxBeginThread(ThreadManager,GetSafeHwnd());
}
//************************************************************
//	函数名:GetIP
//	功能描述:获取IP,并将初始IP分别保留在dStartIP与dEndIP 
//				如果是从文件导入IP则保存在dFileIP
//	参数:无
//	返回:bool
//************************************************************
bool CJIEMIANDlg::GetIP()
{
	//单IP
	if(nIpWay == 0)
	{
		m_SingleIP.GetAddress(dStartIP);
		return true;
		
	}
	//多IP
	if(nIpWay == 1)
	{
		m_sIP.GetAddress(dStartIP);
		m_eIP.GetAddress(dEndIP);
		if(dEndIP<dStartIP)
		{
			MessageBox("请正确填写的IP地址",_T("错误"),MB_ICONWARNING|MB_OK);
			GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
			return false;
		}
		return true;
	}
	//文件读取
	if(nIpWay == 2)
	{
		CStdioFile m_file;
		CString strIP;
		CString f[4];
		//		char temp;
		//int nLen,nSeg=0;
		CString FileName;
		GetDlgItemText(IDC_IPFILE,FileName);
		if(!m_file.Open(FileName,CFile::modeRead,NULL))
		{
			MessageBox("打不开文件",_T("错误"),MB_ICONWARNING|MB_OK);
			GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
			return false;
		}
		nLineCount = 0;
		while(m_file.ReadString( strIP ))
		{
			strIP.TrimLeft();
			strIP.TrimRight();
			if(strIP.IsEmpty())
				continue;
			int index1 = strIP.Find('.');
			int index2 = strIP.Find('.',index1+1);
			int index3 = strIP.Find('.',index2+1);
			if ((index1 == -1)||(index2 == -1)||(index3 == -1))
			{
				AfxMessageBox("IP格式有错!");
				GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
				return false;
			}
			f[0] = strIP.Mid(0,index1);
			f[1] = strIP.Mid(index1+1,index2 - index1 - 1);
			f[2] = strIP.Mid(index2+1,index3 - index2 - 1);
			f[3] = strIP.Mid(index3+1,strIP.GetLength() - index3);
			
			//对每个IP验证
			for(int i = 0 ; i<4 ;i++)
			{
				if(atoi(f[i])>255 || atoi(f[i])<0)
				{
					MessageBox("文件IP数据错误",_T("错误"),MB_ICONWARNING|MB_OK);
					GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
					return false;
				}
				else
				{
					if(i != 3)
					{
						
						dFileIP[nLineCount] += f[i];
						dFileIP[nLineCount] += ".";
					}
					else
					{
						dFileIP[nLineCount] += f[i];
					}
					
				}
				
				
			}			
			
			nLineCount++;
		}
		return true;
	}
	return true;
	
}

//************************************************************
//	函数名:IPChange
//	功能描述:可以将DWORD的IP转换成CString型的IP
//	参数:DWORD IP
//	返回:CString str
//************************************************************
CString IPChange(DWORD IP)
{
	CString str;
	str.Format("%d.%d.%d.%d",
		HIBYTE(HIWORD(IP)),LOBYTE(HIWORD(IP)),
		HIBYTE(LOWORD(IP)),LOBYTE(LOWORD(IP)));
	return str;	
}
//************************************************************
//	函数名:ThreadScan
//	功能描述:扫描单个IP的端口的线程函数
//	参数:LPVOID pParam
//	返回:UINT
//************************************************************
UINT ThreadScan(LPVOID pParam)
{
	threadInfo *thInfo = (threadInfo*)pParam;
	CString strIPAddress = thInfo->strIP;
	HTREEITEM hItem = thInfo->hCurItem;
	//在内部重新打开互斥量
	HANDLE hCounterIn=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"sam sp 44");
	WaitForSingleObject(hCounterIn,INFINITE);
	UINT v=m_nCounter;
	m_nCounter++;
	ReleaseMutex(hCounterIn);
	CloseHandle(hCounterIn);
	int IpPort;
	IpPort=v;
	CString pPort;
	pPort.Format("%d",IpPort);
	SetDlgItemText(hDlg,IDC_SCANIP,strIPAddress);
	SetDlgItemText(hDlg,IDC_SCANPORT,pPort);
	if (bIsSYN)
	{
		CSYNPing ObjSYN;
		//CSYPing::Ping 返回2时表示正在开放此端口
		if (ObjSYN.Ping((LPSTR)(LPCSTR)strIPAddress,IpPort) == 2)
		{
			m_GbTree->InsertItem(pPort,1,1,hItem);
			m_GbTree->Expand(hItem,TVE_EXPAND);
		}
	}
	else
	{
		struct sockaddr_in sin;
		// Create the local socket
		SOCKET sd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
		if (sd == INVALID_SOCKET) 
		{
			SetDlgItemText(hDlg,IDC_PINGIP,"error");
			printf("Create socket error!");
			return 1;
		}
		// Connect to the victim IP Address
		sin.sin_family=AF_INET;
		sin.sin_addr.s_addr=inet_addr((LPSTR)(LPCSTR)strIPAddress);
		sin.sin_port=htons((short)IpPort);
		if (connect (sd, (struct sockaddr *)&sin, sizeof (sin)) == SOCKET_ERROR) 
		{
			//	AfxMessageBox("Connect the remote IP error!");
			closesocket (sd);
			return 1;
		}
		else
		{
			m_GbTree->InsertItem(pPort,1,1,hItem);
			m_GbTree->Expand(hItem,TVE_EXPAND);
			
		}
		closesocket (sd);
	}
	
	return TRUE;
}
//************************************************************
//	函数名:ThreadManager
//	功能描述:组织ping和扫描多个IP
//	参数:LPVOID pParam
//	返回:UINT
//************************************************************
UINT ThreadManager(LPVOID pParam)
{
	CJIEMIANDlg *dlg=(CJIEMIANDlg*)AfxGetApp()->GetMainWnd();
	HANDLE hCounter=NULL;
	if( (hCounter=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"sam sp 44"))==NULL)
	{
		//如果没有其他进程创建这个互斥量,则重新创建
		hCounter = CreateMutex(NULL,FALSE,"sam sp 44");
	}
	if (!bIsScanByhand)
	{
		PingHost();
		dlg->m_pingIP.SetWindowText("");
		
		CWinThread *pT[201];
		HANDLE hThread[201];
		int v;
		if(nStartPort==nEndPort)
		{
			v=1;
		}
		else
		{
			v=(nEndPort-nStartPort+1)/nThreadCount;
			if((nEndPort-nStartPort+1)%nThreadCount > 0)
			{
				v++;
			}
		}
		HTREEITEM tempItem = m_GbTree->GetNextItem(m_hRoot,TVGN_NEXT);
		while(tempItem != NULL)
		{
			int nImage,nSelectedImage ;
			if (!m_GbTree->GetItemImage(tempItem,nImage,nSelectedImage))
			{
				tempItem = m_GbTree->GetNextItem(tempItem,TVGN_NEXT);
				continue;
			}
			if (nImage==3)
			{
				tempItem = m_GbTree->GetNextItem(tempItem,TVGN_NEXT);
				continue;
			}
			CString strTemp = m_GbTree->GetItemText(tempItem);
			m_nCounter = nStartPort;
			for(int i=0; i <= v; i++)
			{
				int nThreadCounter=0;//每次批量创建的线程实际个数,最后一次是一个余数值
				int k;
				for(k=0;k<nThreadCount;k++)
				{
					if(m_nCounter>nEndPort)
					{
						break;
					}
					if (bIsStopPing)
					{
						SetDlgItemText(hDlg,IDC_SCANIP,"");
						SetDlgItemText(hDlg,IDC_SCANPORT,"");
						AfxEndThread(0);
						break;
					}
					//内循环计数
					nThreadCounter++;
					dlg->m_progressScan.StepIt();
					threadInfo thInfo;
					thInfo.hCurItem = tempItem;
					thInfo.strIP = strTemp;
					
					pT[k]=AfxBeginThread((AFX_THREADPROC)ThreadScan,&thInfo);
					hThread[k]=pT[k]->m_hThread;
					
					Sleep(10);
				}
				
				hThread[k]=NULL;//非常重要,如果不加这一句的话,会经常出错,有点奇怪
				//如果k=0,表示没有开启线程
				if(k!=0)
				{
					//WaitForMultipleObjects(nThreadCounter,hThread,TRUE,INFINITE);
					WaitForMultipleObjects(nThreadCounter,hThread,TRUE,500);
				}
				
			}
			tempItem = m_GbTree->GetNextItem(tempItem,TVGN_NEXT);
		}
		CloseHandle(hCounter);
		//设置提示
		//	dlg->StateInital();
		dlg->m_startScan.EnableWindow(true);
		dlg->m_stopScan.EnableWindow(false);
		SetDlgItemText(hDlg,IDC_SCANIP,"");
		SetDlgItemText(hDlg,IDC_SCANPORT,"");
		AfxEndThread(0);
		return TRUE;
	}
	else
	{
		if (!bIsSpecScan)
		{
			PingHost();
			dlg->m_pingIP.SetWindowText("");
			return true;
		}
		if (bIsFocused == false)
			return TRUE;
		//先清除原来的扫描结果
		HTREEITEM tempItem1 = m_hCurrentItem;
		if (m_GbTree->GetParentItem(tempItem1)!=NULL)
			return true;
		HTREEITEM tempItem2 =  m_GbTree->GetChildItem(tempItem1);
		while(tempItem2 != NULL)
		{
			m_GbTree->DeleteItem(tempItem2);
			tempItem2 = m_GbTree->GetChildItem(tempItem1);
		}
		CWinThread *pT[201];
		HANDLE hThread[201];
		int v;
		if(nStartPort==nEndPort)
		{
			v=1;
		}
		else
		{
			v=(nEndPort-nStartPort+1)/nThreadCount;
			if((nEndPort-nStartPort+1)%nThreadCount > 0)
			{
				v++;
			}
		}
		
		int nImage,nSelectedImage ;
		if (!m_GbTree->GetItemImage(tempItem1,nImage,nSelectedImage))
		{
			return true;
		}
		if (nImage==3)
		{
			return true;
		}
		CString strTemp = m_GbTree->GetItemText(tempItem1);
		m_nCounter = nStartPort;
		
		for(int i=0; i <= v; i++)
		{
			int nThreadCounter=0;//每次批量创建的线程实际个数,最后一次是一个余数值
			int k;
			for(k=0;k<nThreadCount;k++)
			{
				if (bIsStopPing)
				{
					SetDlgItemText(hDlg,IDC_SCANIP,"");
					SetDlgItemText(hDlg,IDC_SCANPORT,"");
					AfxEndThread(0);
					break;
				}
				if(m_nCounter>nEndPort)
				{
					break;
				}
				
				//内循环计数
				nThreadCounter++;
				dlg->m_progressScan.StepIt();
				threadInfo thInfo;
				thInfo.hCurItem = tempItem1;
				thInfo.strIP = strTemp;
				
				pT[k]=AfxBeginThread((AFX_THREADPROC)ThreadScan,&thInfo);
				hThread[k]=pT[k]->m_hThread;
				
				Sleep(10);
			}
			
			hThread[k]=NULL;//非常重要,如果不加这一句的话,会经常出错,有点奇怪
			//如果k=0,表示没有开启线程
			if(k!=0)
			{
				//WaitForMultipleObjects(nThreadCounter,hThread,TRUE,INFINITE);
				WaitForMultipleObjects(nThreadCounter,hThread,TRUE,500);
			}
			
		}
		
		CloseHandle(hCounter);
		//设置提示
		//	dlg->StateInital();
		dlg->m_startScan.EnableWindow(true);
		dlg->m_stopScan.EnableWindow(false);
		SetDlgItemText(hDlg,IDC_SCANIP,"");
		SetDlgItemText(hDlg,IDC_SCANPORT,"");
		AfxEndThread(0);
		return TRUE;
	}
	
	return TRUE;
}

//************************************************************
//	函数名:PingHost
//	功能描述:单线程ping主机
//	参数:无
//	返回:void
//************************************************************
void PingHost()
{
	CJIEMIANDlg *dlg=(CJIEMIANDlg*)AfxGetApp()->GetMainWnd();
	CString strTempIP;
	int flag = 0;
	if (nIpWay == 0)
	{
		strTempIP = IPChange(dStartIP);
		if (bIsSYN)
		{
			CSYNPing ObjSYNPing;
			flag = ObjSYNPing.Ping((LPSTR)(LPCSTR)strTempIP,21);
		}
		else
		{
			CPing ObjPing;
			flag = ObjPing.Ping((LPSTR)(LPCSTR)strTempIP);
			ObjPing.Cleanup();
		}
		if(flag)
		{
			m_GbTree->InsertItem(strTempIP,2,2);
		}
		else
		{
			if (!bIsShowResHost)
			{
				m_GbTree->InsertItem(strTempIP,3,3);
			}
		}
		m_GbTree->Expand(m_hCurrentItem,TVE_EXPAND);
		dlg->m_pingIP.SetWindowText(strTempIP);
	}
	if (nIpWay == 1)
	{
		DWORD i = dStartIP;
		while (i <= dEndIP)
		{
			if (bIsStopPing)
			{
				AfxEndThread(0);
				return;
			}
			strTempIP = IPChange(i);
			if (bIsSYN)
			{
				CSYNPing ObjSYNPing;
				flag = ObjSYNPing.Ping((LPSTR)(LPCSTR)strTempIP,21);
			}
			else
			{
				CPing ObjPing;
				flag = ObjPing.Ping((LPSTR)(LPCSTR)strTempIP);
				ObjPing.Cleanup();
			}
			if(flag)
			{
				m_GbTree->InsertItem(strTempIP,2,2);
			}
			else
			{
				if (!bIsShowResHost)
				{
					m_GbTree->InsertItem(strTempIP,3,3);
				}
			}
			m_GbTree->Expand(m_hCurrentItem,TVE_EXPAND);
			dlg->m_pingIP.SetWindowText(strTempIP);
			i++;
		}
	}
	if (nIpWay == 2)
	{
		for (int i = 0; i < nLineCount; i++)
		{
			if (bIsStopPing)
			{
				AfxEndThread(0);
				return;
			}
			if (bIsSYN)
			{
				CSYNPing ObjSYNPing;
				flag = ObjSYNPing.Ping((LPSTR)(LPCSTR)dFileIP[i],21);
			}
			else
			{
				CPing ObjPing;
				flag = ObjPing.Ping((LPSTR)(LPCSTR)dFileIP[i]);
				ObjPing.Cleanup();
			}
			if(flag)
			{
				m_GbTree->InsertItem(dFileIP[i],2,2);
			}
			else
			{
				if (!bIsShowResHost)
				{
					m_GbTree->InsertItem(strTempIP,3,3);
				}
			}
			m_GbTree->Expand(m_hCurrentItem,TVE_EXPAND);
			dlg->m_pingIP.SetWindowText(dFileIP[i]);
		}
	}
}

//************************************************************
//	函数名:GetPort
//	功能描述:获取端口,并将初始端口分别保留在nStartPort与nEndPort 
//	参数:无
//	返回:bool
//************************************************************
bool CJIEMIANDlg::GetPort()
{
	if(((CButton *)GetDlgItem(IDC_SINGLE_PORT))->GetCheck())
	{
		nStartPort = nEndPort = GetDlgItemInt(IDC_PORT);
		return true;
	}
	else
	{
		nStartPort = GetDlgItemInt(IDC_SPORT);
		nEndPort = GetDlgItemInt(IDC_EPORT);
		if (nEndPort < nStartPort)
		{
			MessageBox("请正确填写的端口",_T("错误"),MB_ICONWARNING|MB_OK);
			GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
			GetDlgItem(IDC_SPORT)->SetWindowText("");
			GetDlgItem(IDC_EPORT)->SetWindowText("");
			GetDlgItem(IDC_SPORT)->SetFocus();
			return false;	
		}
		return true;
		
	}
}

void CJIEMIANDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	TVITEM item = pNMTreeView->itemNew;
	m_hCurrentItem = item.hItem;
	*pResult = 0;
}

//************************************************************
//	函数名:OnSetfocusTree1
//	功能描述:树型控件获取焦点 
//	参数:系统消息参数
//	返回:void
//************************************************************
void CJIEMIANDlg::OnSetfocusTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	bIsFocused = true;
	*pResult = 0;
}

//************************************************************
//	函数名:OnKillfocusTree1
//	功能描述:树型控件失去焦点 
//	参数:系统消息参数
//	返回:void
//************************************************************
void CJIEMIANDlg::OnKillfocusTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	bIsFocused = true;
	*pResult = 0;
}

void CJIEMIANDlg::OnScanSelected() 
{
	// TODO: Add your control notification handler code here
	bIsSpecScan = true;
	bIsScanByhand = true;
	GetPort();
	nThreadCount = m_iThCount;
	m_progressScan.SetRange32(0,nEndPort - nStartPort + 1);
	m_progressScan.SetPos(0);
	AfxBeginThread(ThreadManager,GetSafeHwnd());
}


void CJIEMIANDlg::OnWaysyn() 
{
	// TODO: Add your control notification handler code here
	bIsSYN = true;
}

void CJIEMIANDlg::OnWayconn() 
{
	// TODO: Add your control notification handler code here
	bIsSYN = false;
}

void CJIEMIANDlg::OnStopScan() 
{
	bIsStopPing = true;
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_START_SCAN)->EnableWindow(true);
	GetDlgItem(IDC_STOP_SCAN)->EnableWindow(false);
}

void CJIEMIANDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	m_Tree.DeleteAllItems();
	m_hRoot = m_Tree.InsertItem("主机IP",0,0);
	m_progressScan.SetPos(0);
}

//************************************************************
//	函数名:OnSavescan	
//	功能描述:保存当前扫描列表至指定文本文件 
//	参数:无
//	返回:void
//************************************************************
void CJIEMIANDlg::OnSavescan() 
{
	// TODO: Add your control notification handler code here
	CFileDialog Dlg(FALSE,NULL,"ScanResult.txt",OFN_HIDEREADONLY,"文本文件(*.txt)|*.txt||",this);
	Dlg.m_ofn.lpstrTitle = "保存扫描结果";
	if (Dlg.DoModal() == IDOK)
	{
		CString filePath = Dlg.GetPathName();
		//UpdateData(false);
		if (filePath == "")
		{
			AfxMessageBox("请先选择创建位置和文件名!");
			return;
		}
		CStdioFile txtFile;
		if (!txtFile.Open( filePath, CFile::modeCreate | CFile::modeWrite | CFile::typeText ))
		{
			AfxMessageBox("不能创建文件!");
			return;
		}
		if (bIsSYN)
		{
			txtFile.WriteString("SYN扫描");
		}
		else
		{
			txtFile.WriteString("connect扫描");
		}
		txtFile.WriteString("\n\n");
		HTREEITEM tempItem1 = m_Tree.GetNextItem(m_hRoot,TVGN_NEXT);
		HTREEITEM tempItem2;
		while (tempItem1 != NULL)
		{
			int nImage,nSelectedImage;
			m_Tree.GetItemImage(tempItem1,nImage,nSelectedImage);
			CString ItemText = m_Tree.GetItemText(tempItem1);
			CString total = ItemText;
			if (nImage == 2)
			{
				total += ":通\n";
				txtFile.WriteString(total);
			}
			else
			{
				total += ":断\n";
				txtFile.WriteString(total);
			}
			tempItem2 = m_Tree.GetChildItem(tempItem1);
			while (tempItem2 != NULL)
			{
				ItemText = m_Tree.GetItemText(tempItem2);
				total = "开放端口:";
				total += ItemText;
				total += "\n";
				txtFile.WriteString(total);
				tempItem2 = m_Tree.GetNextItem(tempItem2,TVGN_NEXT);
			}
			txtFile.WriteString("\n");
			tempItem1 = m_Tree.GetNextItem(tempItem1,TVGN_NEXT);

		}
		txtFile.Close();
	}
}

void CJIEMIANDlg::OnShowReshost() 
{
	// TODO: Add your control notification handler code here
	bIsShowResHost = !bIsShowResHost;
}